<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>token</title>
</head>

<body>
  <h3>表单提交</h3>
  <form action="http://localhost:8080/api/authenticate" method="post">
    <input type="text" name="name" >
    <input type="text" name="password">
    <input name="" value="提交" type="submit">提交</input>
  </form>

  <h3>根据用户信息获取token</h3>
  <input type="text" name="name" id="name">
  <input type="text" name="password" id="password">
  <button name="" value="提交" onclick="GetToken()">提交</button>

  <h3>登录验证</h3>
  <button value="提交" onclick="verification()">登录验证</button>
  <p id="token"></p>


  <button value="退出" onclick="signout()">退出登录</button>

  <script type="text/javascript">

    function GetToken() {
      var name = $('#name').val();
      var password = $('#password').val();
      console.log(name)
      $.ajax({
        url: 'http://localhost:8080/api/authenticate',
        cache:false,
        data: {
          name: name,
          password: password
        },
        dataType: 'JSON',
        async: false, //请求是否异步，默认为异步
        type: 'POST',
        success: function(list) {
          console.log(list)
          $('#token').text(list.token)
          window.localStorage.setItem('token', list.token);
        },
        error: function() {}
      });
    }

    function verification() {
      var token = window.localStorage.getItem('token');
      $.ajax({
        url: 'http://localhost:8080/api/users',
        cache:false,
        beforeSend: function(request) {
          request.setRequestHeader("x-access-token", token);
        },
        dataType: 'JSON',
        async: false, //请求是否异步，默认为异步
        type: 'GET',
        success: function(list) {
          if(list.success){
            console.log('没过期'+list.success);
          }else{
            console.log('过期了'+list.success);
            location.href='/check';
          }
        },
        error: function() {}
      });
    }
    function signout(){
      window.localStorage.clear();
      var token = window.localStorage.getItem('token');
      $.ajax({
        url: 'http://localhost:8080/api/singout',
        cache:false,
        beforeSend: function(request) {
          request.setRequestHeader("x-access-token", token);
        },
        async: false, //请求是否异步，默认为异步
        type: 'GET',
        success: function(list) {
          if(list.success){
            console.log('退出不成功'+list.success);
          }else{
            console.log('退出成功'+list.success);
            location.href='/check';
          }
        },
        error: function() {}
      });
    }
  </script>
  <script src="/jquery.min.js" charset="utf-8"></script>
</body>

</html>
